package org.ls.petclinic.web;

import org.ls.petclinic.dao.OwnerDao;
import org.ls.petclinic.dao.impl.OwnerDaoImpl;
import org.ls.petclinic.pojo.Owner;
import org.ls.petclinic.untils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 浪矢先生
 * @description
 * @create 2022-12-05 23:47
 */
@WebServlet(urlPatterns = "/addOwner")
public class AddOwner extends BaseServlet{

    private OwnerDao ownerDao=new OwnerDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
            int result = ownerDao.add(new Owner(
                    null,
                    req.getParameter("name"),
                    req.getParameter("address"),
                    req.getParameter("city"),
                    req.getParameter("phone")));
        JdbcUtils.commitAndClose();
        if (result>0){
            req.setAttribute("msg", "修改主人信息成功!");
        }else {
            req.setAttribute("msg", "修改主人信息失败!");
        }
        //页面转发
        req.getRequestDispatcher("/jsp/getPets.jsp").forward(req, resp);
//        resp.sendRedirect(req.getContextPath() + "/jsp/getUnHealthyPet.jsp");
    }
}
